#    Copyright 2012 Tri-County Electric Cooperative, St. Matthews, South Carolina
#    This file is part of COOPWeb.
#    COOPWeb is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#    COOPWeb is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License
#    along with COOPWeb.  If not, see <http://www.gnu.org/licenses/>.

#************************BEGIN Add Include Files to Path************************
import sys
NeededPaths=['/opt/coopweb/djangoproject/include', '/opt/coopweb/djangoproject']
for path in NeededPaths:
    if(not path in sys.path):
        sys.path.append(path)
#************************END   Add Include Files to Path************************
import os, csv, shutil, datetime, time, reportlab, djangoproject.settings, random
from django.contrib.gis.db import models
from django.contrib.gis.geos import *
from django.http import HttpResponse
from django.contrib import admin
from django.template import Context, loader
import djangoproject.settings
from models import *
from django import forms
import django
import djangoproject
from django.shortcuts import redirect
import COOPWeb_Form, COOPWeb_FormMeter, COOPWeb_FormAlarm, COOPWeb_DynamicTemplate













def AlarmTable(request):
    """View that allows dispatch/others to arm/disarm alarms and to acknowledge alarm alerts in a tabular form"""
    if(not request.is_secure()):
        return redirect(djangoproject.settings.COOPWebSecureWebsiteName)
    if(not request.user.is_authenticated()):
        return redirect(djangoproject.settings.COOPWebSecureWebsiteName)

    import datetime
    Now = datetime.datetime.now()

    Alarms=Alarm.objects.filter(UpdateFromCOOPIPDateTime__gte = Now-datetime.timedelta(hours=1))
    
    Username = request.user.username

    SpecialBuildFormDataDict={}
    SpecialBuildFormDataDict['Alarms'] = Alarms
    SpecialBuildFormDataDict['request'] = request

    form=COOPWeb_Form.BuildDynamicForm(request, 'AlarmTable', SpecialBuildFormDataDict)
    COOPWeb_FormAlarm.ProcessAlarmTableForm(form, Username)
    form=COOPWeb_FormAlarm.PopulateAlarmTableForm(form, Alarms)
    #raise(Exception(str(form.data)))
    COOPWeb_DynamicTemplate.UpdateAlarmTableTemplate(Username, Alarms)
    t = loader.get_template('/opt/coopweb/djangoproject/templates/dynamic_alarmtable_' + Username + '.html')
    c = Context({'form': form})
    return HttpResponse(t.render(c))

















